package cn.meizhu.pet.controller;

import cn.meizhu.org.domain.Department;
import cn.meizhu.org.query.DepartmentQuery;
import cn.meizhu.pet.domain.SearchMasterMsg;
import cn.meizhu.pet.query.SearchMasterMsgQuery;
import cn.meizhu.pet.service.ISearchMasterMsgService;
import cn.meizhu.util.AjaxResult;
import cn.meizhu.util.QueryResult;
import com.sun.deploy.net.HttpResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


    /*@PostMapping("/processed")
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.findAll();
    }*/


    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        seachMasterMsgService.publish(searchMasterMsg,request);
        return AjaxResult.me();
    }

    @PostMapping
    public QueryResult<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery){
        return seachMasterMsgService.queryPage(searchMasterMsgQuery);
    }

    @PutMapping
    @ApiOperation(value = "部门添加或者修改", notes = "有id就是修改，没有id就是新增")
    public AjaxResult addAndUpdDepartment(@RequestBody SearchMasterMsg searchMasterMsg){
        try{
            if (searchMasterMsg.getId() == null){
                seachMasterMsgService.save(searchMasterMsg);
            }else {
                seachMasterMsgService.update(searchMasterMsg);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "这是部门删除")
    public AjaxResult delDepartmentById(@PathVariable("id") Long id){
        try {
            seachMasterMsgService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询部门中指定数据")
    public SearchMasterMsg findDepartmentById(@PathVariable("id") Long id){
        return seachMasterMsgService.findById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询部门全部数据")
    public List<SearchMasterMsg> findDepartments(Long id,HttpServletRequest request){
        return seachMasterMsgService.findAll(id,request);
    }


    /**
     * 批量删除
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelDepartmentByIds(@RequestBody Long[] ids){
        try {
            seachMasterMsgService.patchDelByIds(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setMessage(e.getMessage());
        }
    }
}
